package managesystem;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class addDistractionServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	/**
	 * Afhandelen van de informatie van het formulier
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		HttpSession session = req.getSession();
		Student currStud = (Student) session.getAttribute("currStud");
		String distraction = req.getParameter("distraction").trim();
		boolean addedDistraction = false;
		String error = "This distraction is already in your list.";
		boolean valid = false;
		if(distraction.length() != 0){
			valid = true;
			addedDistraction = currStud.addDistraction(distraction);
		}
		else{
			error = "You didn't fill in a distraction.";
		}
		
		if(valid == false){
			session.setAttribute("error",error );
		}
		else {if(addedDistraction == false){
			session.setAttribute("error",error );
		}
			else {
				session.setAttribute("message", "A new distraction "+distraction+" "+" is added succesfully.");
				session.setAttribute("currStud", currStud);
				StudentManager.add(currStud);// update in de database
			}
		}
		try {
			String fromMobile = (String) session.getAttribute("fromMobile");
			if(fromMobile.equals("true")){
				resp.sendRedirect("/index-mobile.jsp");
			}
			else{
				resp.sendRedirect("/index.jsp?id=dashboard");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


